package com.witframework.baseapp.security.service.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.witframework.baseapp.security.Role;
import com.witframework.baseapp.security.BaseUser;
import com.witframework.baseapp.security.UserRoles;
import com.witframework.baseapp.security.dao.UserRolesDao;
import com.witframework.baseapp.security.service.UserRolesService;
import com.witframework.core.ServiceCallbackWithoutResult;
import com.witframework.core.TransactionTemplate;
import com.witframework.core.logic.BaseService;
import com.witframework.core.logic.ISimpleBaseService;

@Service("userRolesService")
public class UserRolesServiceImpl extends BaseService implements UserRolesService{
	UserRolesDao<UserRoles> dao;

	@Autowired
	public void setDao(UserRolesDao<UserRoles> dao) {
		this.dao = dao;
		super.setDao(dao);
	}
	private void otherMethod1(){
		System.out.println("otherMethod1 called.");
		ISimpleBaseService service=com.witframework.core.WitFrameWork.getSimpleBaseService(Role.class);
		Role r=new Role();
		r.setName("r00000000");
		service.save(r);		
	}
	private void otherMethod2(){
		System.out.println("otherMethod2 called.");
		ISimpleBaseService service=com.witframework.core.WitFrameWork.getSimpleBaseService(Role.class);		
		Role r=new Role();
		r.setName("r00000000");
		service.save(r);		
		service.delete(r);
	}
	private void otherMethod3(){
		System.out.println("otherMethod3 called.");
		ISimpleBaseService service=com.witframework.core.WitFrameWork.getSimpleBaseService(Role.class);				
		Role r=new Role();
		r.setName("r00000000");
		service.save(r);		
		r.setName("r00000000XXXXXXX");		
		service.save(r);
		r=new Role();
		r.setName("r00000000");
		service.save(r);		
		r=new Role();
		r.setName("r00000000");
		String id=service.save(r);			
		service.get(id);
		service.findBy("name", "r00000000");
		throw new RuntimeException("error");
	}
	public void test(){
		ISimpleBaseService userService=com.witframework.core.WitFrameWork.getSimpleBaseService(BaseUser.class);
		BaseUser u=new BaseUser();
		u.setUsername("a");
		u.setName("u");
		u.setPassword("p");
		userService.save(u);
		BaseUser u1=new BaseUser();
		u1.setUsername("a1");
		u1.setName("u1");
		u1.setPassword("p");
		userService.save(u1);		
		ISimpleBaseService roleService=com.witframework.core.WitFrameWork.getSimpleBaseService(Role.class);
		Role r=new Role();
		r.setName("r");
		String rid=roleService.save(r);
		System.out.println("rid="+roleService.getAllSize());
		
		new TransactionTemplate().doInTransaction(
				new ServiceCallbackWithoutResult(){
					public void execute(){							
						otherMethod1();
						otherMethod2();
					}
				});
		
		Role r1=new Role();
		r1.setName("r1");
		roleService.save(r1);
		Role r2=new Role();
		r2.setName("r2");
		roleService.save(r2);
		System.out.println("Role size="+roleService.getAllSize());
		
		UserRolesService URService=(UserRolesService)com.witframework.core.WitFrameWork.getBean("userRolesService");
		UserRoles ur=new UserRoles();
		ur.setUser(u);
		ur.setRole(r);
		URService.save(ur);
		
		Map filter =new HashMap();
		filter.put("role.id", rid);
		List rList=URService.findBy(filter);
		System.out.println(rid+"="+rList.size());
	}
	public void testTransaction() throws SQLException{
//		ISimpleBaseService service=com.witframework.core.WitFrameWork.getSimpleBaseService(Role.class);
//		System.out.println("***************Role size="+service.getAllSize());
//		new TransactionTemplate().doInTransaction(
//				new ServiceCallbackWithoutResult(){
//					public void execute(){							
//						otherMethod1();
//						otherMethod2();
//						
//					}
//		});
//		otherMethod3();
		com.witframework.datasource.SmartDataSource ds=
			(com.witframework.datasource.SmartDataSource)com.witframework.core.WitFrameWork.getBean("dataSource");
		ds.getConnection();
//		service=com.witframework.core.WitFrameWork.getSimpleBaseService(Role.class);
//		System.out.println("***************Role size="+service.getAllSize());
	}
	public static void main(String[] s){
		//new UserRolesServiceImpl().test();
		for(int i=0;i<100000;i++){
			System.out.println("***************R"+i);
			try{
				new UserRolesServiceImpl().testTransaction();				
			}catch(Exception e){
				
			}
		}
		try {
			Thread.sleep(100000);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
